import MoveCube from "./MoveCube";
import { utils } from "../../../../utils/CommonUtil";
import MoveCubePos from "./MoveCubePos";
import LevelBaseControl from "../../../LevelCommon/LevelBaseControl";
import EnumData from "../../../../Enum/EnumData";

export default class MoveCubeLock extends Laya.Script3D {
    /** 位置节点*/
    private posRoot: Laya.Sprite3D;
    /** 当前选中的cube*/
    public curSelectCube: MoveCube;
    public curAllCube: Array<MoveCube> = new Array<MoveCube>();
    /** 当前是否有方块正在移动*/
    private isMoving: boolean = false;
    /** 当前是否已经打开了*/
    private isFinish: boolean = false;

    private mowner: Laya.Sprite3D;
    onStart() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.posRoot = this.mowner.getChildByName("poses") as Laya.Sprite3D;
        this.curAllCube = utils.CommonUtils.GetComponentsInDirectChildren<MoveCube>(this.mowner, MoveCube)
        for (let index = 0; index < this.curAllCube.length; index++) {
            this.curAllCube[index].MoveCubeLock = this;
        }
    }

    /** 设置选中cube*/
    public SetSelectCube(moveCube: MoveCube): void {
        if (this.curSelectCube != null) {
            this.curSelectCube.SetSelect(false);
        }
        this.curSelectCube = moveCube;
        if (this.curSelectCube != null) {
            this.curSelectCube.SetSelect(true);
        }
    }
    /** 点击方向*/
    public InputDir(moveCubeDir: EnumData.MoveCubeDirEnum): void {
        if (this.curSelectCube == null || this.isMoving || this.isFinish) {
            return;
        }
        if (!this.IsCanMove(moveCubeDir)) {
            console.log("不可以移动！！！！！！");
            return;
        }
        /** 可以移动*/
        console.log("可以移动！！！！！！");
        this.curSelectCube.Move(moveCubeDir);
    }
    /** 判断当前选择的物体 能否向一个方向移动*/
    public IsCanMove(moveDir: EnumData.MoveCubeDirEnum): boolean {
        for (let index = 0; index < this.curSelectCube.curMoveCubePos.length; index++) {
            let item: MoveCubePos = this.curSelectCube.curMoveCubePos[index];
            let row: number = item.Row;
            let col: number = item.col;
            switch (moveDir) {
                case EnumData.MoveCubeDirEnum.up:
                    row -= 1;
                    break;
                case EnumData.MoveCubeDirEnum.bottom:
                    row += 1;
                    break;
                case EnumData.MoveCubeDirEnum.left:
                    col -= 1;
                    break;
                case EnumData.MoveCubeDirEnum.right:
                    col += 1;
                    break;
                default:
                    break;
            }
            let targetPos: Laya.Sprite3D = this.posRoot.getChildByName("pos" + row + col) as Laya.Sprite3D;
            /** 如果位置被占了*/
            if (this.IsStandPos(row, col) || targetPos == null) {
                return false;
            }
        }

        return true;
    }
    /** 检测一个位置是否被占着*/
    public IsStandPos(raw: number, col: number): boolean {
        for (let index = 0; index < this.curAllCube.length; index++) {
            let item = this.curAllCube[index];
            /** 不检测当前选择的自身是否占着*/
            if (item == this.curSelectCube) {
                continue;
            }
            /** 被占着*/
            if (item.IsStandPos(raw, col)) {
                return true;
            }
        }

        return false;
    }
    /** 获得位置*/
    public GetPos(raw: number, col: number): Laya.Vector3 {
        return (this.posRoot.getChildByName("pos" + raw + col) as Laya.Sprite3D).transform.position.clone() as Laya.Vector3;
    }
    /** 获得一个位置某个方位的位置*/
    public GetPosByDir(raw: number, col: number, dirEnum: EnumData.MoveCubeDirEnum): Laya.Vector3 {
        switch (dirEnum) {
            case EnumData.MoveCubeDirEnum.up:
                raw -= 1;
                break;
            case EnumData.MoveCubeDirEnum.bottom:
                raw += 1;
                break;
            case EnumData.MoveCubeDirEnum.left:
                col -= 1;
                break;
            case EnumData.MoveCubeDirEnum.right:
                col += 1;
                break;
            default:
                break;
        }
        return this.GetPos(raw, col);
    }
    /** 设置正在移动*/
    public SetIsMoving(isMoving: boolean) {
        this.isMoving = isMoving;
    }
    /** 设置完成了*/
    public SetFinish() {
        this.isFinish = true;
        let curLevelControl: LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();
        curLevelControl.DoInteractObjLogic(this.owner.name + "_MoveCubeUnlock");
    }
    /** 根据位置信息 和方向 获得方向移动后的位置信息*/
    public GetMovePosByDir(moveCubePos: Array<MoveCubePos>, dir: EnumData.MoveCubeDirEnum): Array<MoveCubePos> {
        let length = moveCubePos.length;
        let targetMovePoses: Array<MoveCubePos> = new Array<MoveCubePos>(length);
        for (let i = 0; i < length; i++) {
            let curPos: MoveCubePos = moveCubePos[i];
            let targetPos: MoveCubePos = null;
            switch (dir) {
                case EnumData.MoveCubeDirEnum.up:
                    targetPos = new MoveCubePos(curPos.Row - 1, curPos.col);
                    break;
                case EnumData.MoveCubeDirEnum.bottom:
                    targetPos = new MoveCubePos(curPos.Row + 1, curPos.col);
                    break;
                case EnumData.MoveCubeDirEnum.left:
                    targetPos = new MoveCubePos(curPos.Row, curPos.col - 1);
                    break;
                case EnumData.MoveCubeDirEnum.right:
                    targetPos = new MoveCubePos(curPos.Row, curPos.col + 1);
                    break;
                default:
                    break;
            }
            targetMovePoses[i] = targetPos;
        }
        return targetMovePoses;
    }
}